from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
import matplotlib.pyplot as plt
import matplotlib
font = {'family': 'SimHei'}
matplotlib.rc('font', **font)

class TbSpider:
    def __init__(self):
        self.brower = webdriver.Edge(executable_path='msedgedriver.exe')
        self.brower.get("https://www.taobao.com")
        self.info = []

    # 手动登录
    def login(self, p):
        if self.brower.find_element_by_link_text("亲，请登录"):
            self.brower.find_element_by_link_text("亲，请登录").click()
            print('15秒内完成扫码')
            try:
                locator_user = (By.CLASS_NAME, 'site-nav-user')
                WebDriverWait(self.brower, 15, 1).until(EC.presence_of_element_located(locator_user))
                print('登录成功')
            except:
                print('扫码超时')
        for page in range(p):
            self.search(page)

    # 搜索、解析
    def search(self,page):
        if page+1 == 1:  # 首页搜索
            try:
                self.brower.find_element_by_id("q").send_keys("铁锅")
            except:
                self.brower.find_element_by_class_name("search-combobox-input").send_keys("铁锅")
            self.brower.find_element_by_xpath("//div[@class='search-button']/button").click()
            print('第1页')
        else: # 翻页
            try:
                locator = (By.XPATH, '//div[@class="wraper"]')
                WebDriverWait(self.brower, 10).until(EC.presence_of_element_located(locator))
                self.brower.find_elements_by_xpath('//a[@class="J_Ajax num icon-tag"]')[-1].click()
                print(f'跳至第{page+1}页')
                time.sleep(2)
            except:
                print('翻页出错')
        # 解析
        title = self.brower.find_elements_by_css_selector('div[class="row row-2 title"]')
        price = self.brower.find_elements_by_css_selector('div[class="price g_price g_price-highlight"]')
        sell_num = self.brower.find_elements_by_css_selector('div[class="deal-cnt"]')
        for i, j, k in zip(title, price, sell_num):
            tit, pri, num = i.text, j.text, k.text[:-3]
            if '万' in num:
                num = int(num[:-2])*10000
            elif '+' in num:
                num = int(num[:-1])
            else:
                num = int(num)
            if [tit,int(pri[1:-3]),num] not in self.info:
                self.info.append([tit,int(pri[1:-3]),num])

def tongji(items):
    ls = []
    for i in items:
        ls += [i[1]]*i[2]
    # ls = list(filter(lambda x: 50<=x <= 500 and x!=119, ls))
    plt.hist(ls, bins=100)
    # plt.xlim(0, 500)
    plt.ylabel('销量总和')
    plt.title('铁锅价格分布')
    plt.show()

if __name__ == '__main__':
    pn = 20  # 爬20页数据
    tbspider = TbSpider()
    tbspider.login(pn)
    tongji(tbspider.info)
